<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of shippingcom
 *
 * @author hassanali
 */
class sliders extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/slider_model', 'adv');
    }

    public function index() {
        $data['result'] = $this->adv->get();
        $this->themes->render('sliders/index', $data);
    }

    public function view($id = FALSE) {
        if ($id) {
            $falid_id = $this->adv->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $data['result'] = $falid_id;
            $this->themes->render('sliders/view', $data);
        } else {
            show_404();
        }
    }

    private function upload_file($input) {
        $config['upload_path'] = ADS_PATH . '/temp/';
        $config['file_name'] = md5(time());
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1024';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload($input)) {
            return $this->upload->display_errors();
        } else {
            $filedata = $this->upload->data();
            $this->session->set_userdata($input, $filedata['file_name']);
            return true;
        }
    }

    public function edit($id = false) {
        if ($id) {
            $pass['languages'] = get_languages();
            $id_id = $this->adv->is_found(array('id' => $id), array(), 'row', __CLASS__);
            $pass['advertise'] = $id_id;
            if ($this->input->post()) {
                $this->session->set_userdata('post', $this->input->post());
                $this->form_validation->set_rules('url', lang('label_url'), 'trim|required');
                if ($this->form_validation->run()) {
                    $this->upload_file("advertise");
                    if ($this->session->userdata("advertise")) {
                        if (copy(ADS_PATH . "/temp/" . $this->session->userdata("advertise"), ADS_PATH . "/" . $this->session->userdata("advertise"))) {
                            unlink(ADS_PATH . "/temp/" . $this->session->userdata("advertise"));
                            $data['image'] = $this->session->userdata("advertise");
                            unlink(ADS_PATH . $id_id->image);
                        }
                    }
                    $data['user_edit'] = $this->admin_id;
                    $data['type'] = 'slider';
                    $data['lang'] = $this->input->post('language');
                    if ($this->adv->save($data, array('id' => $id), TRUE)) {
                        $this->session->unset_userdata('post');
                        $this->session->unset_userdata("advertise");
                        $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_edit_success'));
                    } else {
                        get_save_error();
                    }
                } else {
                    get_validation_errors();
                }
            } else {
                $this->themes->render('sliders/edit', $pass);
            }
        } else {
            show_404();
        }
    }

    public function add() {
        if ($this->input->post()) {
            $this->session->set_userdata('post', $this->input->post());
            $this->form_validation->set_rules('code', lang('label_image'), 'trim|required');
            if ($this->form_validation->run()) {
                $upload = $this->upload_file("advertise");
                if ($this->session->userdata("advertise")) {
                    if (copy(ADS_PATH . "/temp/" . $this->session->userdata("advertise"), ADS_PATH . "/" . $this->session->userdata("advertise"))) {
                        unlink(ADS_PATH . "/temp/" . $this->session->userdata("advertise"));
                        $data['image'] = $this->session->userdata("advertise");
                    }
                } else {
                    die($upload);
                    $this->themes->set_message(array('url' => current_url(), 'type' => 'error', 'message' => $upload));
                }
                $data['user_add'] = $this->admin_id;
                $data['user_edit'] = $this->admin_id;
                $data['lang'] = $this->input->post('language');
                $data['type'] = 'slider';
                if ($this->adv->save($data)) {
                    $this->session->unset_userdata('post');
                    $this->themes->set_message(array('url' => __CLASS__, 'type' => 'success', 'message' => 'message_add_success'));
                } else {
                    get_save_error();
                }
            } else {
                get_validation_errors();
            }
        } else {
            $pass['languages'] = get_languages();
            $this->themes->render('sliders/add', $pass);
        }
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        // valid if no item select 
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'delete': {
                    $this->adv->delete(array('id' => $id));
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
                }
                break;
            case 'no_trash': {
                    $data['trash'] = 'NO';
                    $this->adv->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete_final': {
                    $id_id = $this->adv->is_found(array('id' => $id), array(), 'row', $this->agent->referrer());
                    $this->adv->delete(array('id' => $id), FALSE);
                    unlink(ADS_PATH . $id_id->image);
                    $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                show_404();
                break;
        }
    }

    public function no_trash($id = false) {
        if ($id) {
            $falid_id = $this->adv->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            $data['trash'] = 'NO';
            if ($this->adv->save($data, array('id' => $id, 'trash' => 'YES'), TRUE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete_final($id = false) {
        if ($id) {
            $falid_id = $this->adv->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            if ($this->adv->delete(array('id' => $id), FALSE)) {
                $ref = $this->agent->referrer();
                @unlink(ADS_PATH . $falid_id->image);
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete($id = FALSE) {
        if ($id) {
            $falid_id = $this->adv->is_found(array('id' => $id), array(), 'row', __CLASS__);
            if ($this->adv->delete(array('id' => $id))) {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $this->themes->set_message(array('url' => $this->agent->referrer(), 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function trash($offset = false) {
        $this->pass['result'] = $this->adv->get(false, FALSE, 'result', FALSE, FALSE, TRUE);
        $this->themes->render('sliders/trash', $this->pass);
    }

}

?>
